package freaks.terah.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JLabel;

import freaks.terah.model.FileIO;

public class RessourceCountPanel extends JPanel {
	
	public static final int HORIZONTAL_LAYOUT = 0;
	public static final int SQUARE_LAYOUT = 1;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int layout;
	private boolean displayZeroes;
	private ArrayList<Image> images = new ArrayList<Image>();
	private ArrayList<int[]> coords = new ArrayList<int[]>();

	/**
	 * Create the panel.
	 */
	public RessourceCountPanel() {
		setLayout(null);
		layout = SQUARE_LAYOUT;
		displayZeroes = false;
	}
	
	public RessourceCountPanel(int aLayout, boolean aDisplayZeroes) {
		setLayout(null);
		layout = aLayout;
		displayZeroes = aDisplayZeroes;
	}
	
	public void setRessource(int[] ressources, String specialDescription)
	{
		setRessource(ressources, specialDescription, null);
	}
	
	public void setRessource(int[] ressources, String specialDescription, int[] compared)
	{
		removeAll();
		images.clear();
		coords.clear();
		if (ressources == null || ressources.length != 9)
		{
			if (specialDescription != null)
			{
				//Set Text
			}
		} else {
			int startingPosition = 6;
			
			if (layout == SQUARE_LAYOUT)
			{
				int resCount = 0;
				for (int i = 0; i < 9; i++)
				{
					if (ressources[i] != 0)
						resCount++;
				}

				if (resCount < 4)
				{
					startingPosition = 26;
				} else if (resCount < 7)
				{
					startingPosition = 16;
				}
			}
			
			int total = 0;
			for (int i = 0; i < 9; i++)
			{
				int count = ressources[i];
				if (count != 0 || displayZeroes)
				{
					int x = 0;
					int y = 0;
					if (layout == SQUARE_LAYOUT)
					{
						x = 6 + 36*(total%3);
						y = startingPosition + 20*(total/3);
					}
					else
					{
						x = 6 + 36*(total); 
					    y = 9;
			        }
						
					try {
						images.add(FileIO.getImage(i));
						coords.add(new int[]{x,y});
					} catch (IOException e) {
						e.printStackTrace();
					}
					
					JLabel newNumber = new JLabel();
					if (layout == SQUARE_LAYOUT)
					    newNumber.setBounds(22 + 36*(total%3), startingPosition + 20*(total/3), 18, 16);
					else
						newNumber.setBounds(22 + 36*(total), 9, 18, 16);
					newNumber.setText(String.valueOf(count));
					
					if (compared != null)
					{
						if (count < compared[i])
							newNumber.setForeground(Color.RED);
					    if (count > compared[i])
						    newNumber.setForeground(Color.GREEN);
					}
					
					if (count < 0)
					{
						newNumber.setForeground(Color.RED);
					    newNumber.setFont(newNumber.getFont().deriveFont(Font.BOLD));
					}
					
					add(newNumber);
					
					total++;
				}
			}
			
			validate();
			repaint();
			return;
		}
		
		JLabel newLabel = new JLabel();
		if (layout == SQUARE_LAYOUT)
			newLabel.setBounds(6, 26, 100, 16);
		else
			newLabel.setBounds(6, 6, 100, 16);
		newLabel.setText("None");
		add(newLabel);
		
		validate();
		repaint();
		return;
	}
	
	public void paint(Graphics g) {
		super.paint(g);
		for (int i = 0; i < images.size(); i++)
		{
			 g.drawImage(images.get(i), coords.get(i)[0], coords.get(i)[1], null);
		}
    }
}
